<template>
    <div class="app-container calendar-list-container">
        <div class="filter-container">
            <el-button v-if="training_class_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加
            </el-button>
        </div>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
                  highlight-current-row style="width: 100%">
            <el-table-column align="center" label="" v-if="dialogStatus == 'update'">
                <template scope="scope">
                    <span>{{ scope.row.fid }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="班级名称">
                <template scope="scope">
                    <span>{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="班级类型">
                <template scope="scope">
                    <span>{{ scope.row.type }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="教练">
                <template scope="scope">
                    <span>{{ scope.row.coach1 }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="教练2">
                <template scope="scope">
                    <span>{{ scope.row.coach2 }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="分管领导">
                <template scope="scope">
                    <span>{{ scope.row.leader }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="部门">
                <template scope="scope">
                    <span>{{ scope.row.deptId }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="开始日期">
                <template scope="scope">
                    <span>{{ scope.row.beginDate }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="结束日期">
                <template scope="scope">
                    <span>{{ scope.row.endDate }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="状态">
                <template scope="scope">
                    <span>{{ scope.row.bizStatus }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="200">
                <template scope="scope">
                    <el-button v-if="training_class_upd" size="mini" type="success"
                               @click="handleUpdate(scope.row)">编辑
                    </el-button>
                    <!--<el-button v-if="training_class_del" size="mini" type="danger"-->
                               <!--@click="handleDelete(scope.row)">删除-->
                    <!--</el-button>-->
                </template>
            </el-table-column>
        </el-table>
        <div v-show="!listLoading" class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page.sync="listQuery.page"
                           :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="cancel('form')">
            <el-form :model="form" :rules="rules" ref="form" size="mini" label-width="100px">
                        <el-form-item label="编码" prop="fid" v-if="dialogStatus == 'update'">
                            <el-input v-model="form.fid" placeholder="" :disabled="true"></el-input>
                        </el-form-item>
                        <el-form-item label="班级名称" prop="name">
                            <el-input v-model="form.name" placeholder="班级名称"></el-input>
                        </el-form-item>
                        <el-form-item label="班级类型" prop="type">
                            <el-input v-model="form.type" placeholder="班级类型"></el-input>
                        </el-form-item>
                        <el-form-item label="教练" prop="coach1">
                            <el-input v-model="form.coach1" placeholder="教练"></el-input>
                        </el-form-item>
                        <el-form-item label="教练2" prop="coach2">
                            <el-input v-model="form.coach2" placeholder="教练2"></el-input>
                        </el-form-item>
                        <el-form-item label="分管领导" prop="leader">
                            <el-input v-model="form.leader" placeholder="分管领导"></el-input>
                        </el-form-item>
                        <el-form-item label="部门" prop="deptId">
                            <el-input v-model="form.deptId" placeholder="部门"></el-input>
                        </el-form-item>
                        <el-form-item label="开始日期" prop="beginDate">
                            <el-input v-model="form.beginDate" placeholder="开始日期"></el-input>
                        </el-form-item>
                        <el-form-item label="结束日期" prop="endDate">
                            <el-input v-model="form.endDate" placeholder="结束日期"></el-input>
                        </el-form-item>
                        <el-form-item label="状态" prop="bizStatus">
                            <el-input v-model="form.bizStatus" placeholder="状态"></el-input>
                        </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel('form')">取 消</el-button>
                <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
                <el-button v-else type="primary" @click="update('form')">修 改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { fetchList, addObj, putObj, delObj } from '@/api/training/training_class'
    import waves from '@/directive/waves/index.js' // 水波纹指令
    import { mapGetters } from 'vuex'

    export default {
        name: 'table_training_class',
        directives: {
            waves
        },
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    limit: 20
                },
                rules: {
                    fid: [
                        { required: true, message: '请输入', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入班级名称', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请输入班级类型', trigger: 'blur' }
                    ],
                    coach1: [
                        { required: true, message: '请输入教练', trigger: 'blur' }
                    ],
                    coach2: [
                        { required: true, message: '请输入教练2', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '请输入创建时间', trigger: 'blur' }
                    ],
                    createUser: [
                        { required: true, message: '请输入创建人', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '请输入修改时间', trigger: 'blur' }
                    ],
                    leader: [
                        { required: true, message: '请输入分管领导', trigger: 'blur' }
                    ],
                    deptId: [
                        { required: true, message: '请输入部门', trigger: 'blur' }
                    ],
                    beginDate: [
                        { required: true, message: '请输入开始日期', trigger: 'blur' }
                    ],
                    endDate: [
                        { required: true, message: '请输入结束日期', trigger: 'blur' }
                    ],
                    bizStatus: [
                        { required: true, message: '请输入状态', trigger: 'blur' }
                    ],
                    version: [
                        { required: true, message: '请输入版本', trigger: 'blur' }
                    ],
                    delFlag: [
                        { required: true, message: '请输入删除标识', trigger: 'blur' }
                    ],
                },
                form: {
                    fid: '',
                    name: '',
                    type: '',
                    coach1: '',
                    coach2: '',
                    createTime: '',
                    createUser: '',
                    updateTime: '',
                    leader: '',
                    deptId: '',
                    beginDate: '',
                    endDate: '',
                    bizStatus: '',
                    version: '',
                    delFlag: '',
                },
                dialogFormVisible: false,
                dialogStatus: '',
                    training_class_add: false,
                    training_class_upd: false,
                    training_class_del: false,
                textMap: {
                    update: '编辑',
                    create: '创建'
                },
                tableKey: 0
            }
        },
        computed: {
            ...mapGetters([
                'permissions'
            ])
        },
        filters: {
            statusFilter(status) {
                const statusMap = {
                    1: '有效',
                    0: '无效'
                }
                return statusMap[status]
            }
        },
        created() {
            this.getList()
            this.training_class_add = this.permissions['training_class_add']
            this.training_class_upd = this.permissions['training_class_upd']
            this.training_class_del = this.permissions['training_class_del']
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.records
                    this.total = response.data.total
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.limit = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleDelete(row) {
                delObj(row)
                        .then(response => {
                            this.dialogFormVisible = false
                            this.getList()
                            this.$notify({
                                title: '成功',
                                message: '删除成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
            },
            handleUpdate(row) {
                this.dialogStatus = 'update'
                this.dialogFormVisible = true
                   this.form.fid = row.fid
                   this.form.name = row.name
                   this.form.type = row.type
                   this.form.coach1 = row.coach1
                   this.form.coach2 = row.coach2
                   this.form.createTime = row.createTime
                   this.form.createUser = row.createUser
                   this.form.updateTime = row.updateTime
                   this.form.leader = row.leader
                   this.form.deptId = row.deptId
                   this.form.beginDate = row.beginDate
                   this.form.endDate = row.endDate
                   this.form.bizStatus = row.bizStatus
                   this.form.version = row.version
                   this.form.delFlag = row.delFlag
            },
            handleCreate() {
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            create(formName) {
                const set = this.$refs
                        set[formName].validate(valid => {
                    if (valid) {
                        addObj(this.form)
                                .then(() => {
                                    this.dialogFormVisible = false
                                    this.getList()
                                    this.$notify({
                                        title: '成功',
                                        message: '创建成功',
                                        type: 'success',
                                        duration: 2000
                                    })
                                    const set = this.$refs
                                    set[formName].resetFields()
                                })
                    } else {
                        return false
                    }
                })
            },
            cancel(formName) {
                this.dialogFormVisible = false
                const set = this.$refs
                set[formName].resetFields()
            },
            update(formName) {
                const set = this.$refs
                        set[formName].validate(valid => {
                    if (valid) {
                        this.dialogFormVisible = false
                        this.form.password = undefined
                        putObj(this.form).then(() => {
                            this.dialogFormVisible = false
                            this.getList()
                            const set = this.$refs
                            set[formName].resetFields()
                            this.$notify({
                                title: '成功',
                                message: '修改成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
                    } else {
                        return false
                    }
                })
            }
        }
    }
</script>
